package com.lottery.im.module.result.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import com.lottery.im.R;
import com.lottery.im.api.result.entity.AllGameSelectInfo;
import java.util.List;

public class GameSelectGridAdapter extends BaseAdapter {
  private List<AllGameSelectInfo.ChildGameTypeListBean> mDataList;
  private OnGameItemClickListener mOnGameItemClickListener;
  private Context mContext;

  public GameSelectGridAdapter(Context context,
      List<AllGameSelectInfo.ChildGameTypeListBean> list) {
    mDataList = list;
    mContext = context;
  }

  @Override public int getCount() {
    return mDataList.size();
  }

  @Override public Object getItem(int position) {
    if (position >= mDataList.size() || position < 0) return null;
    return mDataList.get(position);
  }

  @Override public long getItemId(int position) {
    return position;
  }

  @Override public View getView(final int position, View v, ViewGroup parent) {
    final ViewHolder holder;
    if (v == null) {
      v = LayoutInflater.from(mContext).inflate(R.layout.item_game_select_grid, parent, false);
      holder = new ViewHolder();
      holder.cb = v.findViewById(R.id.cb_game_name);
      v.setTag(holder);
    } else {
      holder = (ViewHolder) v.getTag();
    }
    holder.cb.setText(mDataList.get(position).getGameTypeName());
    holder.cb.setChecked(mDataList.get(position).isSelect());
    holder.cb.setOnClickListener(v1 -> {
      if (null != mOnGameItemClickListener) {
        mOnGameItemClickListener.OnGameItemClick(position);
      }
    });
    return v;
  }

  class ViewHolder {
    protected View layout;
    protected CheckBox cb;
  }

  public void setOnGameItemClickListener(OnGameItemClickListener onGameItemClickListener) {
    mOnGameItemClickListener = onGameItemClickListener;
  }

  public interface OnGameItemClickListener {
    void OnGameItemClick(int position);
  }
}
